package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.ReservationDetailTable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author system
 * @since 2024-10-23 22:22:57
 */
@Component
public class ReservationDetailTableSpecifications {

    public static Specification<ReservationDetailTable> queryList(ReservationDetailTable reservationDetailTable) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (reservationDetailTable.getDetailId() != null) {
                predicates.add(builder.like(root.get("detailId" ).as(String.class), "%" + reservationDetailTable.getDetailId() + "%" ));
            }
            if (reservationDetailTable.getReservationId() != null) {
                predicates.add(builder.equal(root.get("reservationId" ).as(Integer.class), reservationDetailTable.getReservationId()));
            }
            if (reservationDetailTable.getReservationType() != null) {
                predicates.add(builder.like(root.get("reservationType" ).as(String.class), "%" + reservationDetailTable.getReservationType() + "%" ));
            }
            if (reservationDetailTable.getReserverName() != null) {
                predicates.add(builder.like(root.get("reserverName" ).as(String.class), "%" + reservationDetailTable.getReserverName() + "%" ));
            }
            if (reservationDetailTable.getContactPhone() != null) {
                predicates.add(builder.like(root.get("contactPhone" ).as(String.class), "%" + reservationDetailTable.getContactPhone() + "%" ));
            }
            if (reservationDetailTable.getReservationTime() != null) {
                predicates.add(builder.equal(root.get("reservationTime" ).as(Date.class), reservationDetailTable.getReservationTime()));
            }
            if (reservationDetailTable.getReviewStatus() != null) {
                predicates.add(builder.like(root.get("reviewStatus" ).as(String.class), "%" + reservationDetailTable.getReviewStatus() + "%" ));
            }
            if (reservationDetailTable.getRating() != null) {
                predicates.add(builder.equal(root.get("rating" ).as(Integer.class), reservationDetailTable.getRating()));
            }
            if (reservationDetailTable.getIsApproved() != null) {
                predicates.add(builder.like(root.get("isApproved" ).as(String.class), "%" + reservationDetailTable.getIsApproved() + "%" ));
            }
            if (reservationDetailTable.getResponseContent() != null) {
                predicates.add(builder.like(root.get("responseContent" ).as(String.class), "%" + reservationDetailTable.getResponseContent() + "%" ));
            }
            if (reservationDetailTable.getReviewTime() != null) {
                predicates.add(builder.equal(root.get("reviewTime" ).as(Date.class), reservationDetailTable.getReviewTime()));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
